#include <iostream>

using namespace std;

//int func()
//{
//	int i, j, k = 0;
//	for (i = 0, j = -1; j = 0; i++, j++)
//	{
//		k++;
//	}
//
//	return k;
//}
//
//int main()
//{
//	cout << (func());
//
//	return 0;
//}


// 
#include <vector>


class Solution1 {
public:
    int minOperations(vector<int>& nums, int x) {
        int sum = 0;
        for (auto e : nums)
        {
            sum += e;
        }
        if (sum < x)
            return -1;

        int target = sum - x, tmp = 0;
        int left = 0, right = 0, n = nums.size(), ans = -1;
        for (right = 0; right < n; right++)
        {
            tmp += nums[right];

            while (tmp > target)
                tmp -= nums[left++];

            if (target == tmp)
                ans = max(ans, right - left + 1);
        }

        return ans == -1 ? ans : n - ans;
    }
};


//int main()
//{
//    vector<int> v({ 8828, 9581, 49, 9818, 9974, 9869, 9991, 10000, 10000, 10000, 9999, 9993, 9904, 8819, 1231, 6309 });
//    Solution1 test;
//    test.minOperations(v, 134365);
//    return 0;
//}


/////////////////////////

class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        int hash[100000] = { 0 };
        int kinds = 0;

        int ans = 0, left = 0, right = 0;
        while (right < fruits.size())
        {
            hash[fruits[right]]++;
            if (hash[fruits[right++]] == 1)
                kinds++;

            while (kinds > 2)
            {
                hash[fruits[left]]--;
                if (hash[fruits[left++]] == 0)
                    kinds--;
            }

            ans = max(ans, right - left);
        }

        return ans;
    }
};

int main()
{
    Solution test;
    vector<int> v({ 0,1,2,2 });
    test.totalFruit(v);

    return 0;
}